import { createStore } from 'vuex'

export default createStore({
  state: {
  },
  getters: {
  },
  mutations: {
  },
  actions: {
  },
  modules: {
    user: {
      namespaced: true,
      state: {
        userInfo: null,
        roomInfo: null
      },
      mutations: {
        setUserInfo(state, info) {
          state.userInfo = info;
        },
        setRoomInfo(state, info) {
          state.roomInfo = info;
        }
      },
      actions: {
        // 可以添加异步操作，例如从 localStorage 加载用户信息
        loadUserInfo({ commit }) {
          const token = localStorage.getItem('token');
          if (token) {
            // 假设你可以从 token 或其他地方获取用户信息
            // 这里只是一个示例，实际应用中可能需要调用API获取完整用户信息
            const userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}'); // 假设用户信息也存储在localStorage
            if (userInfo) {
              commit('setUserInfo', userInfo);
            }
          }
        },
        setUserInfo({ commit }, info) {
          commit('setUserInfo', info);
        },
        setRoomInfo({ commit }, info) {
          commit('setRoomInfo', info);
        }
      },
    },
  }
})
